from django import forms

from apps.forms import FormMixin
from apps.articles.models import Article, Banner


class ArticleCategoryForm(forms.Form):
    name = forms.CharField(max_length=10)

class EditArticleCategoryForm(forms.Form, FormMixin):
    pk = forms.IntegerField(required=True, error_messages={
        'required': '必须传入分类的id'
    })
    name = forms.CharField(max_length=10, error_messages={
        'max_length': '最大长度不超过10个'
    })

class WriteArticleForm(forms.ModelForm, FormMixin):
    category = forms.IntegerField()

    class Meta:
        model = Article
        exclude = ['category', 'author', 'pub_time']

class PublishCommentForm(forms.Form,FormMixin):
    article_id = forms.IntegerField(error_messages={
        'required': '请输入文章id！'
    })
    content = forms.CharField(error_messages={
        'required': '请输入评论内容！'
    })

class AddBannerForm(forms.ModelForm, FormMixin):

    def clean(self):
        super(AddBannerForm, self).clean()
        count = Banner.objects.count()
        if count >= 6:
            raise forms.ValidationError(message='轮播图超出最大数量！')

    class Meta:
        model = Banner
        fields = ['priority', 'link_to', 'image_url']
        error_messages = {
            'priority': {
                'required': '请输入优先级！',
                'invalid': '请输入数字！'
            },
            'link_to': {
                'required': '请输入跳转链接！',
                'invalid': '请输入正确的url格式！'
            },
            'image_url': {
                'required': '请传入图片！',
                'invalid': '请先传入图片！'
            }
        }

class EditBannerForm(forms.ModelForm, FormMixin):
    pk = forms.IntegerField(error_messages={
        'required': '请输入轮播图id！',
        'invalid': '请输入数字！'
    })

    class Meta:
        model = Banner
        fields = ['priority', 'link_to', 'image_url']
